package cn.zmwh.manage.admin.modules.upms.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.constants.Constants;
import cn.zmwh.common.security.annotation.PreRole;
import cn.zmwh.manage.admin.modules.upms.mapper.SysUserMapper;
import cn.zmwh.manage.admin.modules.upms.pojo.entity.SysUser;
import cn.zmwh.manage.admin.modules.upms.pojo.request.SysUserSaveRequest;
import cn.zmwh.manage.admin.modules.upms.pojo.vo.SysMenuNode;
import cn.zmwh.manage.admin.modules.upms.pojo.vo.SysUserVO;
import cn.zmwh.manage.admin.modules.upms.service.SysMenuService;
import cn.zmwh.manage.admin.modules.upms.service.SysRoleService;
import cn.zmwh.manage.admin.modules.upms.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dmzmwh
 * @since 2025-03-11
 */
@Api(tags = "用户")
@RestController
public class SysUserController {


    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    public SysMenuService sysMenuService;
    @Autowired
    public SysUserRoleService sysUserRoleService;


    @GetMapping("/api/user/info")
    @ApiOperation(value = "用户信息")
    public Result<SysUserVO> userInfo(@ApiIgnore @RequestHeader(Constants.UID) Long uid) throws Exception {
        SysUser sysUser = sysUserMapper.selectById(uid);
        if (sysUser == null) {
            return Result.failed("用户不存在");
        }
        SysUserVO sysUserVO = new SysUserVO();
        BeanUtil.copyProperties(sysUser, sysUserVO);
        sysUserVO.setPassword(null);


        String mobile = sysUser.getMobile();
        Set<String> roleList = sysRoleService.listByUser(uid, mobile);
        sysUserVO.setRoleList(roleList);
        List<SysMenuNode> sysMenuNodes = sysMenuService.treeList(uid, mobile);
        sysUserVO.setMenuList(sysMenuNodes);
        return Result.ok(sysUserVO);
    }

    @PostMapping("/api/user/save")
    @ApiOperation(value = "用户信息")
    @PreRole(role = "ADMIN", name = "创建用户")
    public Result save(@RequestBody SysUserSaveRequest request) throws Exception {
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(request, sysUser);
        sysUser.setEnabled(1);
        String password = request.getPassword();
        if (StrUtil.isNotBlank(password)) {
            sysUser.setPassword(passwordEncoder.encode(password));
        }
        sysUserMapper.insert(sysUser);

        Set<Long> roleIds = request.getRoleIds();
        if (CollUtil.isNotEmpty(roleIds)) {
            sysUserRoleService.relevance(sysUser.getId(), roleIds);
        }
        return Result.success();
    }

}

